package com.bawei.park2302.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.appcompat.widget.SearchView
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.utils.utils.entity.RepairBeanItem
import com.bawei.lib_base.utils.utils.mvi.BaseActivity
import com.bawei.lib_base.utils.utils.mvi.UiState
import com.bawei.lib_base.utils.utils.request.RequestType
import com.bawei.park2302.R
import com.bawei.park2302.adapter.RepairAdapter
import com.bawei.park2302.databinding.ActivityRepairBinding
import com.bawei.park2302.park.ParkIntent
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding,RepairIntent,RepairViewModel>() {
    override val binding: ActivityRepairBinding by lazy {
        ActivityRepairBinding.inflate(layoutInflater)
    }
    override val vm: RepairViewModel by viewModels<RepairViewModel>()

    val repairAdapter by lazy { RepairAdapter() }
    var list = mutableListOf<RepairBeanItem>()
    val data = 0
    var flag = false
    var type = ""

    override fun initView() {
        binding.rvRepair.apply {
            adapter = repairAdapter
            layoutManager = LinearLayoutManager(this@RepairActivity)
        }

        binding.repairOrder.setOnClickListener {
            if (flag){
                repairAdapter.data.sortBy { it.repairTime }
                binding.repairOrderiv.setImageResource(R.drawable.ic_arrow_upward)
            }else{
                repairAdapter.data.sortByDescending { it.repairTime }
                binding.repairOrderiv.setImageResource(R.drawable.ic_arrow_downward)
            }
            repairAdapter.notifyDataSetChanged()
            flag =!flag
        }

        binding.ivAdd.setOnClickListener {
            ARouter.getInstance().build("/app/RepairAddActivity").navigation()
        }

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/RepairDetailActivity")
                .withInt("repairId",repairAdapter.data[position].id).navigation()
        }

        binding.btnSearch.setOnClickListener{
            val content = binding.repairSv.text.toString()
            if (type.isEmpty()){
                vm.sendIntent(RepairIntent.repairOrderNumber(content))
            }else{
                vm.sendIntent(RepairIntent.repairOrderSearch(content))
            }
        }

        binding.repairSv.addTextChangedListener { object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                if(s.isNullOrEmpty()){
                    repairAdapter.data.clear()
                    repairAdapter.addData(list)
                    repairAdapter.notifyDataSetChanged()
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }
        } }

        binding.repairFilter.setOnClickListener {
            type = "user"
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                       when(it.type){
                           RequestType.SEARCH ->{
                               repairAdapter.data.clear()
                               val items = it.data as RepairBeanItem
                               repairAdapter.addData(items)
                               repairAdapter.notifyDataSetChanged()
                           }

                           RequestType.SPACE ->{
                               repairAdapter.data.clear()
                               list.clear()
                               val items = it.data as Collection<RepairBeanItem>
                               list.addAll(items)
                               repairAdapter.addData(items)
                               repairAdapter.notifyDataSetChanged()
                           }
                           else -> {}
                       }
                    }
                    else -> {}
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(RepairIntent.repair(data))
    }
}